package org.client;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.client.chat.ClientChatingWindow;
import org.model.Friend;
import org.model.QParentFrame;
import org.model.ViewUpdatable;


public class Peer extends QParentFrame implements WindowListener,ViewUpdatable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public static void main(String[] args) {
		new Peer();
	}
	
	private PeerServerSocket mServerThread;
	private JTextField usernameTxt;
	private ChatServerConnector mChatServer;
	private JList friendlist;
	private JButton chatBtn;
	private JButton loginButton;
	private Vector<Friend> friendVector;
	private String loginName;
	private byte[] key;
	public Peer() {
		super("Peer");
		mainContainer.setLayout(new BorderLayout());
		
		friendlist = new JList();
		mainContainer.add(friendlist);
		friendlist.add("ahmd",new JButton("test"));
		chatBtn = new JButton("chat");
		chatBtn.setEnabled(false);
		mainContainer.add(chatBtn, BorderLayout.SOUTH);
		ButtonHandler btnHandler = new ButtonHandler();
		chatBtn.addActionListener(btnHandler);
		this.addWindowListener(this);
		

		final JPanel panel = new JPanel();
		mainContainer.add(panel, BorderLayout.NORTH);
		usernameTxt = new JTextField();
		panel.add(usernameTxt);
		usernameTxt.setColumns(10);
		
		loginButton = new JButton("login");
		panel.add(loginButton);
		loginButton.addActionListener(btnHandler);
		showWindow();
	}
	private class ButtonHandler implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource() == loginButton)
			{
				mServerThread = new PeerServerSocket();
				loginName = usernameTxt.getText();
				int openPort = mServerThread.connect(loginName);
				mChatServer = new ChatServerConnector(loginName,"",openPort,(ViewUpdatable)Peer.this);
				mChatServer.start();
				loginButton.setEnabled(false);
				usernameTxt.setEnabled(false);
			}
			else if(e.getSource() == chatBtn)
			{
				int index = friendlist.getSelectedIndex();
				Friend friend = friendVector.get(index);
				new ClientChatingWindow(friend,loginName,key);
			}
		}
		
	}
		
	

	@Override
	public void windowClosing(WindowEvent arg0) {
		//System.out.println("i will be killed");
		if(mChatServer != null)
			mChatServer.logOut();
	}

	@Override
	public void updateView(Vector<Friend> friends) {
		friendVector = friends;
		Vector<String> listArray = new Vector<String>();
		Iterator<Friend> iter = friends.iterator();
		while (iter.hasNext()) {
			Friend friend = (Friend) iter.next();
			listArray.add(friend.getName());
		}
		friendlist.setListData(listArray);
		if(listArray.size() > 0)
			chatBtn.setEnabled(true);
		else
			chatBtn.setEnabled(false);
	}


	public void setPrivateKey(byte[] key) {
		this.key = key;
		mServerThread.setKey(key);
		mServerThread.start();
	}

	
}
